<template>
  <view>
    <view class="goods-list">
      <view v-for="goods in goodsList" :key="goods.goods_id" @click="goDetail(goods.goods_id)" class="goods-items">

           <myGoods :goods="goods"></myGoods>
     
      </view>
    </view>
  </view>
</template>

<script>
import { getShopSearchList } from '../../api/request';
  export default {
    data() {
      return {
        // 查询参数
        queryInfo:{
          query: '',
          cid: '',
          pagenum: 1,
          pagesize: 10
        },
        // 总数量，用来实现分页
        total: 0,
        // 商品列表
        goodsList:[],
        // 节流阀
        flags:false
      };
    },
    onLoad(options) {
      console.log(options);
      this.queryInfo.query = options.query || ''
      this.queryInfo.cid = options.cid  || ''
      this.getGoodsList()
    },
    onPullDownRefresh() {
      this.queryInfo.pagenum = 1
      this.total = 0
      this.goodsList = []
      this.getGoodsList(()=>{uni.stopPullDownRefresh()})
    },
    onReachBottom() {
      if(this.flags) return
      if(this.queryInfo.pagenum * this.queryInfo.pagesize > this.total){
        return uni.showMsg("数据加载完毕！")
      }
      this.queryInfo.pagenum++
      this.getGoodsList()      
    },
    methods:{
      // 获取商品列表
      async getGoodsList(cb){
        this.flags = true
       const {data:res} = await getShopSearchList(this.queryInfo)
       if(res.meta.status !== 200) return uni.$showMsg()
       this.flags = false
       cb && cb()
       this.goodsList = [...this.goodsList, ...res.message.goods]
       this.total = res.message.total
      },
      // 跳转到商品详情页面
      goDetail(id){
        uni.navigateTo({
          url:"/subpkg/goods_detail/goods_detail?goods_id=" + id
        })
      }
    },
  }
</script>

<style lang="scss">
.goods-list{
  display: flex;
  flex-wrap: wrap;
  // justify-content: space-around;
  box-sizing: border-box;

  background-color: #efefef;
  .goods-items{
    width: 48%;
    margin: 2px;
  }
}
</style>
